/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web;

import ejb.TiendaliaOrderBeanLocal;
import ejb.UserBeanLocal;
import entities.TiendaliaOrder;
import entities.User;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class AdmUserBean {

    @EJB
    private UserBeanLocal userBean;
    @EJB
    private TiendaliaOrderBeanLocal orderBean;
    private User user;
    private List<TiendaliaOrder> orders;
    private String newPassword;
    private String newPassword2;
    private String oldPassword;

    public AdmUserBean() {
        newPassword = "";
        newPassword2 = "";
    }

    @PostConstruct
    public void init() {
        user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
        orders = orderBean.findByCustomer(user);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<TiendaliaOrder> getOrders() {
        return orders;
    }

    public void setOrders(List<TiendaliaOrder> orders) {
        this.orders = orders;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getNewPassword2() {
        return newPassword2;
    }

    public void setNewPassword2(String newPassword2) {
        this.newPassword2 = newPassword2;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public void editUser() {
        User isUser = userBean.findByCredentials(user.getEmail(), oldPassword);
        if (isUser != null) {
            if (newPassword.equals(newPassword2) && !newPassword.isEmpty()) { // new passwords are not empty and match
                user.setPassword(newPassword);
            } else if (!newPassword.isEmpty()) {    // new passwords are not empty but don't match
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Las nuevas contraseñas no coinciden", ""));
            } else {    //No new passwords
                userBean.update(user);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Se ha actualizado tu perfil", ""));
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Contraseña incorrecta", ""));
        }
    }

    public String disableUser() {
        String outcome = "";
        user.setIsActive(Short.valueOf("0"));
        try {
            userBean.update(user);

            outcome = "index?faces-redirect=true";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "La cuenta " + user.getEmail() + " ha sido dada de baja.", ""));
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "No se pudo dar de baja la cuenta", ""));
        }

        return outcome;
    }
}
